package dialog;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Layer;
import utils.ImageOperationUtils;
import utils.ImageUtils;
import abstraction.ImageOperationDialog;

/**
 * 
 * @author Maxime
 */
public class OpacityLayerDialog extends ImageOperationDialog {

	private JLabel m_OpacityLabel;
	private JSlider m_opacitySlider;
	private JButton m_buttonOpacity;
	private int opacityValue ;
	public static int DEFAULT_OPACITY;
	private Layer m_layer;

	public OpacityLayerDialog(JFrame parent,Layer layer) {
		super(parent, layer.getImage());
		m_layer = layer;
		opacityValue = layer.getAlpha();;
		DEFAULT_OPACITY = layer.getAlpha();
		m_titleLabel.setText("Layer Opacity");
		m_componentPanel.setLayout(new GridLayout(1, 3));
		m_OpacityLabel = new JLabel("Brigtness : ");
		m_opacitySlider = new JSlider(-opacityValue,-opacityValue+255 , 0);
		m_buttonOpacity = new JButton("Reset");

		m_componentPanel.add(m_OpacityLabel);
		m_componentPanel.add(m_opacitySlider);
		m_componentPanel.add(m_buttonOpacity);

		m_opacitySlider.addChangeListener(new SliderListener());
		m_buttonOpacity.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				opacityValue = 0;
				m_opacitySlider.setValue(0);
			}
		});
	}

	private class SliderListener implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent e) {

			JSlider slider = (JSlider) e.getSource();
			int value = slider.getValue();
			opacityValue = value;
			m_layer.setAlpha(DEFAULT_OPACITY+value);
			m_preview = ImageOperationUtils.createOpacityOp(m_current, opacityValue);
			m_imageLabel.setIcon(new ImageIcon(ImageUtils.resizeTo(m_preview,
					m_newWidth, m_newHeight)));
		}
	}
}
